//import org.activiti.engine.*;
//import org.activiti.engine.history.HistoricProcessInstance;
//import org.activiti.engine.task.Task;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.apache.commons.compress.utils.Lists;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;


@RunWith(SpringRunner.class)
@SpringBootTest(classes = test.class)
public class test {

    @Test
    public void testname() {
        List<Integer> integers = Lists.newArrayList();
        integers.add(11);
        integers.add(13);
        integers.add(12);
        integers.add(14);
        List<Integer> integers2 = Lists.newArrayList();
        integers2.add(1);
        integers2.add(14);
        integers2.add(3);
        integers2.add(2);
        List<Integer> integers3 = Lists.newArrayList();
        System.out.println(integers.size());
//        for (Integer integer : integers) {
//            for (int i = integers2.size()-1; i >= 0; i--) {
//                if (integers2.get(i) == integer){
//                    integers3.add(integer);
//                    integers2.remove(i);
//                    continue;
//                }
//            }
//        }

        for (Integer integer : integers) {
            for (int i = 0; i < integers2.size(); i++) {
                if (integers2.get(i) == integer) {
                    integers3.add(integer);
                    integers2.remove(i);
                    continue;
                }
            }
        }
        System.out.println(integers3.toString());
    }

//    @Autowired
//    private RepositoryService repositoryService;
//    @Autowired
//    private RuntimeService runtimeService;
//    @Autowired
//    private TaskService taskService;
//    @Autowired
//    private HistoryService historyService;
//
//    @Test
//    public void init(){
//        try {
//            System.out.println("开始建表。。。");
//            //创建引擎配置对象
//            ProcessEngineConfiguration configuration = ProcessEngineConfiguration.createStandaloneInMemProcessEngineConfiguration();
//            //创建流程引擎对象
//            //目标生成数据库表
//            configuration.setJdbcUrl("jdbc:mysql://localhost:3306/test");
//            configuration.setJdbcDriver("com.mysql.jdbc.Driver");
//            configuration.setJdbcUsername("root");
//            configuration.setJdbcPassword("123456");
//
//            //设置表的生成策略
//            /**
//             public static final String DB_SCHEMA_UPDATE_FALSE = "false";不能自动创建表，需要表存在
//             public static final String DB_SCHEMA_UPDATE_CREATE_DROP = "create-drop";先删除表再创建表
//             public static final String DB_SCHEMA_UPDATE_TRUE = "true";如果表不存在，自动创建表
//             */
//            configuration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
//            ProcessEngine processEngine = configuration.buildProcessEngine();
//            System.out.println(processEngine.getName() + "***********");
//            System.out.println("建表结束。。。");
//        }catch (Exception e){
//            System.out.println("建表失败！！！");
//            e.printStackTrace();
//        }
//    }
//
//    //发布流程
//    @Test
//    public void initProcessEngine2(){
//        try {
////            //获取流程引擎
////            ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
////            //获取仓库服务的实例
////            Deployment deploy = processEngine.getRepositoryService()
////                    .createDeployment()
////                    .addClasspathResource("/processes/test.bpmn").deploy();
////            System.out.println(deploy.getId()+"  "+deploy.getName());
//            /*String filePath = "/processes/test.bpmn";
//            //读取bpmn文件，并发布
//            Deployment deploy = repositoryService.createDeployment().addInputStream("test.xml", is).deploy();
//            ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deploy.getId()).singleResult();
//            Long businessKey = new Double(100000 * Math.random()).longValue();
//            //启动流程
//            runtimeService.startProcessInstanceById(processDefinition.getId(),businessKey.toString());
//            //查询任务实例
//            List<Task> taskList = taskService.createTaskQuery().processDefinitionId(processDefinition.getId()).list();
//            for (Task task : taskList) {
//                System.out.println("task name is  " + task.getName() + " ,task key is " + task.getTaskDefinitionKey());
//            }*/
//            String filePath = "/processes/test.bpmn";
//            InputStream is = Class.class.getClasses().getClass().getResource(filePath).openStream();
//            //部署流程定义
//            repositoryService.createDeployment().addInputStream("test.xml",is).deploy();
//            //启动流程
//            String pid = runtimeService.startProcessInstanceByKey("myProcess").getId();
//            /******************第一个任务开始******************************/
//            List<Task> taskList = taskService.createTaskQuery().taskCandidateGroup("sales").list();
//            for (Task task : taskList) {
//                System.out.println("Following task is available for sales group: " + task.getName());
//                // 认领任务这里由foozie认领，因为fozzie是sales组的成员
//                taskService.claim(task.getId(), "user1");
//            }
//
//            //查看fozzie是否能够获取该任务
//            List<Task> user1 = taskService.createTaskQuery().taskAssignee("user1").list();
//            for (Task task : user1) {
//                System.out.println("the task name is :" + task.getName());
//                // 执行(完成)任务
//                taskService.complete(task.getId());
//            }
//            System.out.println("user1当前的任务数:" +  taskService.createTaskQuery().taskAssignee("gonzo").count());
//            /******************第一个任务结束******************************/
//
//            /******************第二个任务开始******************************/
//            taskList = taskService.createTaskQuery().taskCandidateGroup("management").list();
//            for(Task task : taskList){
//                System.out.println("Following task is available for management group: " + task.getName());
//                taskService.claim(task.getId(), "kermit");
//            }
//
//            //查看kermit是否能够获取改任务
//            taskList = taskService.createTaskQuery().taskAssignee("kermit").list();
//            System.out.println("kermit当前任务数：" + taskList.size());
//            for(Task task : taskList){
//                System.out.println("the task name is :" + task.getName());
//                taskService.complete(task.getId());
//            }
//            System.out.println("kermit当前任务数：" +  taskService.createTaskQuery().taskAssignee("kermit").count());
//
//            /******************第二个任务结束******************************/
//            HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery()
//                    .processInstanceId(pid).singleResult();
//            System.out.println("流程结束时间：" + historicProcessInstance.getEndTime());
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//    }
//
//
//    @Test
//    public void test(){
//        String jsonStr = "";
//        String path = "D:\\WorkBack\\cloudspider\\spider-api-service\\config\\spider-class.json";
//        try {
//            File jsonFile = new File(path);
//            FileReader fileReader = new FileReader(jsonFile);
//            Reader reader = new InputStreamReader(new FileInputStream(jsonFile),"utf-8");
//            int ch = 0;
//            StringBuffer sb = new StringBuffer();
//            while ((ch = reader.read()) != -1) {
//                sb.append((char) ch);
//            }
//            fileReader.close();
//            reader.close();
//            jsonStr = sb.toString();
//        } catch (IOException e) {
//            e.printStackTrace();
//
//        }
//        if (!StringUtils.isEmpty(jsonStr)){
//            JSONArray jsonArray = (JSONArray) JSON.parse(jsonStr);
//            //读取文件获取json
//            JSONArray jsonString = new JSONArray();
//            for (int i = 0; i < jsonArray.size(); i++) {
//                JSONObject json = (JSONObject)jsonArray.get(i);
//                if (json.get("pId").equals("root")){
//                    json
//}

    @Test
    public void init() {
        try {
            System.out.println("开始建表。。。");
            //创建引擎配置对象
            ProcessEngineConfiguration configuration = ProcessEngineConfiguration.createStandaloneInMemProcessEngineConfiguration();
            //创建流程引擎对象
            //目标生成数据库表
            configuration.setJdbcUrl("jdbc:mysql://localhost:3306/test");
            configuration.setJdbcDriver("com.mysql.jdbc.Driver");
            configuration.setJdbcUsername("root");
            configuration.setJdbcPassword("123456");

            //设置表的生成策略
            /**
             public static final String DB_SCHEMA_UPDATE_FALSE = "false";不能自动创建表，需要表存在
             public static final String DB_SCHEMA_UPDATE_CREATE_DROP = "create-drop";先删除表再创建表
             public static final String DB_SCHEMA_UPDATE_TRUE = "true";如果表不存在，自动创建表
             */
            configuration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
            ProcessEngine processEngine = configuration.buildProcessEngine();
            System.out.println(processEngine.getName() + "***********");
            System.out.println("建表结束。。。");
        } catch (Exception e) {
            System.out.println("建表失败！！！");
            e.printStackTrace();
        }
    }

    //回文数
    public boolean isPalindrome(int x) {
        if (x == 0 || x < 0) return false;
        if (x % 10 == 0) return false;
        int result = 0;
        while (x > result) {
            result = result * 10 + x % 10;
            x /= 10;
        }
        return x == result || x == result / 10;
    }

    @Test
    public void test3() {
        int[] ints = {1, 2, 3};
        int[] int2 = {2, 3, 4};
        int[] intersection = intersection(ints, int2);
        for (int i = 0; i < intersection.length; i++) {
            System.out.println(intersection[i]);
        }
    }

    public boolean isPalindrome2(int x) {
        if (x == 0) return true;
        if (x < 0 || x % 10 == 0) return false;
        int reversed = 0;
        while (x > reversed) {
            reversed = reversed * 10 + x % 10;
            x /= 10;
        }
        return x == reversed || x == reversed / 10;
    }


    public List<List<Integer>> fourSum2(int[] nums, int target) {
        List<List<Integer>> result = new ArrayList<>();
        if (nums == null || nums.length < 4) {
            return result;
        }
        Arrays.sort(nums);
        int minValue = nums[0] + nums[1] + nums[2] + nums[3];
        int maxValue = nums[nums.length - 1] + nums[nums.length - 2] + nums[nums.length - 3] + nums[nums.length - 4];

        if (minValue > target || maxValue < target) {
            return result;
        }
        for (int i = 0; i < nums.length - 3; i++) {
            if (i > 0 && nums[i] == nums[i - 1]) {
                continue;
            }
            for (int j = i + 1; j < nums.length - 2; j++) {
                if (j > i + 1 && nums[j] == nums[j - 1]) {
                    continue;
                }
                int left = j + 1;
                int right = nums.length - 1;
                minValue = nums[i] + nums[j] + nums[left] + nums[left + 1];
                maxValue = nums[i] + nums[j] + nums[right] + nums[right - 1];
                if (minValue > target || maxValue < target) {
                    continue;
                }
                while (left < right) {
                    int temp = nums[left] + nums[right] + nums[i] + nums[j];
                    if (temp == target) {
                        List<Integer> l = new ArrayList<>(Arrays.asList(nums[i], nums[j], nums[left], nums[right]));
                        result.add(l);
                        while (left < right && nums[left] == nums[left + 1]) {
                            left++;
                        }
                        while (left < right && nums[right] == nums[right - 1]) {
                            right--;
                        }
                        left++;
                        right--;
                    } else if (temp > target) {
                        right--;
                    } else {
                        left++;
                    }
                }
            }
        }
        return result;
    }

    @Test
    public void test1119() {
        moveZeroes(new int[]{0,1,2,0,12,3});
    }

    //将数组中的0移动到，并且其他数字排序
    public void moveZeroes(int[] nums) {
        if (nums == null || nums.length <= 0) {
            return;
        }

        int temp = 0;
        int n0Index = 0;    // 用于存储 元素不为0 的 最大下标
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != 0) {
                temp = nums[i];
                nums[i] = nums[n0Index];
                nums[n0Index++] = temp;
            }
        }
    }

    //两个数组的交集
    public int[] intersection(int[] nums1, int[] nums2) {
        ArrayList<Integer> ints = new ArrayList<>();
        for (int i = 0; i < nums2.length; i++) {
            ints.add(nums2[i]);
        }
        ArrayList<Integer> integers = new ArrayList<>();
        for (int i = 0; i < nums1.length; i++) {
            if (ints.contains(nums1[i])) {
                if (!integers.contains(nums1[i])) {
                    integers.add(nums1[i]);
                    continue;
                }
            }
        }
        int[] ints1 = new int[integers.size()];
        for (int i = 0; i < integers.size(); i++) {
            ints1[i] = integers.get(i);
        }
        return ints1;
    }

    public int[][] reconstructQueue(int[][] people) {
        // [7,0], [7,1], [6,1], [5,0], [5,2], [4,4]
        // 再一个一个插入。
        // [7,0]
        // [7,0], [7,1]
        // [7,0], [6,1], [7,1]
        // [5,0], [7,0], [6,1], [7,1]
        // [5,0], [7,0], [5,2], [6,1], [7,1]
        // [5,0], [7,0], [5,2], [6,1], [4,4], [7,1]
        Arrays.sort(people, (o1, o2) -> o1[0] == o2[0] ? o1[1] - o2[1] : o2[0] - o1[0]);

        LinkedList<int[]> list = new LinkedList<>();
        for (int[] i : people) {
            list.add(i[1], i);
        }

        return list.toArray(new int[list.size()][2]);
    }


}
